package kr.pe.xissy.soulmate.controller;

import java.util.Locale;

import javax.servlet.http.HttpSession;

import kr.pe.xissy.soulmate.domain.User;
import kr.pe.xissy.soulmate.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping(value = "/user")
public class UserController {

	@Autowired
	protected UserService userService;

	@RequestMapping(value = "/join", method = RequestMethod.POST)
	public String join(Locale locale, Model model, @RequestParam("email") String email, @RequestParam("password") String password, @RequestParam("nickName") String nickName) {
		User user = new User(email, password, nickName);
		userService.save(user);

		model.addAttribute(user);

		return "join";
	}

	@RequestMapping(value = "", method = RequestMethod.GET)
	@PreAuthorize("hasRole('ROLE_USER')")
	public String myInfo(Locale locale, Model model, HttpSession session) {
		model.addAttribute(SecurityContextHolder.getContext().getAuthentication().getPrincipal());

		return "user";
	}

}
